<?php
class TaskModel extends BaseModel {
	/**
	 * 获取当前发布任务
	 *
	 * @param $uid int
	 * @param $limit string
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getMyRelease($uid, $limit = '0,10') {
		$map = array ();
		$map ['uid'] = $uid;
		$map ['status'] = array ('NEQ', '100' );
		return $this->getListByWhere ( $map, 'id,title,create_time,status', $limit, 'create_time DESC' );
	}

	/**
	 * 获取任务列表
	 *
	 * @param $limit string
	 * @return array
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getList($map = null, $limit = null, $fields = 'id,title,status') {
		$tasks = $this->getListByWhere ( $map, $fields, $limit, 'id DESC' );
		$QueueModel = D ( 'Queue' );
		foreach ( $tasks as $key => $task ) {
			if ($task ['status'] == 100) {
				$tasks [$key] ['status_info'] = $QueueModel->getStatus ( $task ['status']);
			} else if($task ['status'] == 3){
				$tasks [$key] ['status_info'] = $this->getStatus ( $task ['status']);
			}else {
				$status = $QueueModel->getStatusById ( $task ['id'] );
				$tasks [$key] ['status'] = $status;
				$tasks [$key] ['status_info'] = $QueueModel->getStatus ( $status );
			}
		}
		return $tasks;
	}

	/**
	 * Get one task
	 *
	 * @param $id int
	 *            task id
	 * @return array
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getOneById($id) {
		return $this->getById ( $id, 'id,title' );
	}

	/**
	 * 根据任务ID 获取任务在不同状态下的详细
	 *
	 * @param $id int
	 * @return array
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function isNowReceive($id) {
		// TODO 加大判断力度
		$map = array ();
		$map ['id'] = $id;
		$map ['status'] = 2;
		// 获取任务详细
		$task = M ( 'Task' )->field ( 'title,mid,fid,bid,start_time,end_time,content,remark,create_time,is_slide,version' )->where ( $map )->find ();
		return $task;
	}

	/**
	 * Access to current state of the number of tasks
	 *
	 * @param $status int
	 *            status
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getStatusCount($status, $uid = 0, $mid = 0) {
		$map = array ();
		$map ['status'] = $status;
		if($uid){
			$map ['uid'] = $uid;
		}
		if($mid){
			$map ['mid'] = $mid;
		}
		return $this->where ( $map )->count ();
	}

	/**
	 * 获取不同状态下其他详细信息
	 *
	 * @param $id int
	 * @param $status int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getOtherDetailByStatus($id, $status) {

	}

	/**
	 * Access to current market floor brand activity
	 *
	 * @param $mid int
	 * @param $fid int
	 * @param $bid int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getBrandCurrentActivity($mid, $fid, $bid, $map = null) {
		$map ['mid'] = $mid;
		$map ['fid'] = $fid;
		$map ['bid'] = $bid;
		$map = array_merge ( $map, $this->getStartToEndTime () );
		return $this->where ( $map )->field ( 'id,title,status,start_time,end_time' )->order('id DESC')->find ();
	}

	/**
	 * @param $mid int
	 * @param $fid int
	 * @param $bid int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getBrandLastActivity($mid, $fid, $bid, $map = null) {
		$map ['mid'] = $mid;
		$map ['fid'] = $fid;
		$map ['bid'] = $bid;
		return $this->where ( $map )->field ( 'id,title,status,start_time,end_time' )->order('id DESC')->find ();
	}
	
	/**
	 * Access to current mall activities
	 *
	 * @param $mid int
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function getMallNowTasks($mid) {
		$map['mid'] = $mid;
		$map['status'] = array('neq', 100);
		$map = array_merge ( $map, $this->getStartToEndTime () );
		return $this->getList ( $map , null,'id,title,status,mid,fid,bid,start_time,end_time', 'id DESC');
	}

	/**
	 * Get today's assigned amount
	 * @author Ice<iceinto@mallog.com.cn>
	 */
	public function dayCount(){
		$map = array();
		$dayTime = $this->getThisDayTime();
		$map['create_time'] = array('GT', $dayTime);
		return $this->where($map)->count();
	}
}